from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from rest_framework.authtoken.models import Token

HTTP_CHOICE = (
    ('HTTP', 'HTTP'),
    ('HTTPS', 'HTTPS')
)

REQUEST_TYPE_CHOICE = (
    ('POST', 'POST'),
    ('GET', 'GET'),
    ('PUT', 'PUT'),
    ('DELETE', 'DELETE')
)

REQUEST_PARAMETER_TYPE_CHOICE = (
    ('form-data', '表单(form-data)'),
    ('raw', '源数据(raw)'),
    ('Restful', 'Restful')
)

PARAMETER_TYPE_CHOICE = (
    ('text', 'text'),
    ('file', 'file')
)

HTTP_CODE_CHOICE = (
    ('200', '200'),
    ('404', '404'),
    ('400', '400'),
    ('502', '502'),
    ('500', '500'),
    ('302', '302'),
)
# down下的代码
# EXAMINE_TYPE_CHOICE = (
#     ('no_check', '不校验'),
#     ('only_check_status', '校验http状态'),
#     ('json', 'JSON校验'),
#     ('entirely_check', '完全校验'),
#     ('Regular_check', '正则校验'),
# )

"""
项目转换 modify by gaojingyu  20201218
"""
# 断言类型
EXAMINE_TYPE_CHOICE = (
    ('assertEqual', '等于'),
    ('assertNotEqual', '不等于'),
    ('assertRegexpMatches', '包含'),
    ('assertNotRegexpMatches', '不包含'),
    ('assertGreater', '大于'),
    ('assertGreaterEqual', '大于等于'),
    ('assertLess', '小于'),
    ('assertLessEqual', '小于等于'),
    ('assertIn', '在列表中'),
    ('assertNotIn', '不在列表中'),
)

# 删除标志
Delete_Flag = (
    ('0', '不删除'),
    ('1', '删除')
)

# 用例分类
Case_Type_CHOICE = (
    ('0', '数据校验'),
    ('1', '业务逻辑'),
    ('2', '疏通测试')
)

# 校验分类
Check_Type_CHOICE = (
    ('0', 'Response校验'),
    ('1', '数据库校验'),

)

PARAM_TYPE = (
    ('0', 'string'),
    ('1', 'datatime'),
    ('2', 'int'),
    ('3', 'list'),

)

# 执行条件
Action_Condition_CHOICE = (
    ('0', 'or'),
    ('1', 'and'),

)

# 认证
authentication = (
    ('0', '无认证'),
    ('1', '加密解密'),
)

# 依赖
relation = (
    ('0', '无依赖'),
    ('1', '有依赖关系'),
)

# 随机数
Random_Number = (
    ('0', '无随机数'),
    ('1', '自动生成随机数'),
)

# 随机数类型
Random_Number_Type = (
    ('0', '自动生成10位随机数'),
    ('1', '自动生成姓名'),
    ('2', 'idcard'),
)

UNIT_CHOICE = (
    ('m', '分'),
    ('h', '时'),
    ('d', '天'),
    ('w', '周'),
)

RESULT_CHOICE = (
    ('PASS', '成功'),
    ('FAIL', '失败'),
)

TASK_CHOICE = (
    ('circulation', '循环'),
    ('timing', '定时'),
)


class Sys_Project(models.Model):
    """
    项目管理表-马嘉璐
    """
    id = models.AutoField(primary_key=True)
    project_name =  models.TextField(blank=True, null=True, verbose_name='项目名称')
    project_name_en =models.TextField(blank=True, null=True, verbose_name='项目英文名称')
    content = models.TextField(blank=True, null=True, verbose_name='描述')
    status = models.CharField(max_length=5, verbose_name='状态') # 1.状态不知有哪些取值？
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    # 需要新建外键关联的主表才能设置外键
    # Project_id = models.ForeignKey(Project, on_delete=models.SET_NULL, null=True, max_length=1024, verbose_name='项目id')

    def __unicode__(self):
        return self.project_name

    def __str__(self):
        return self.project_name

    class Meta:
        db_table = 'sys_project'
        verbose_name = '项目名称'
        verbose_name_plural = '项目名称'


class Sys_Model(models.Model):
    """
    模块表-马嘉璐
    """
    id = models.AutoField(primary_key=True)
    model_name = models.TextField(blank=True, null=True, verbose_name='模块名称')
    create_user = models.TextField(verbose_name='创建用户')
    status = models.CharField(max_length=5, verbose_name='状态', default=False) # 状态不知有哪些取值？
    content = models.TextField(blank=True, null=True, verbose_name='描述', default="")
    testers = models.CharField(max_length=25, verbose_name='测试人员')
    developer = models.CharField(max_length=25, verbose_name='开发人员')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    modify_user = models.TextField(verbose_name='修改用户')
    project = models.ForeignKey(Sys_Project, on_delete=models.SET_NULL, null=True, verbose_name='项目id')  # 移除了 max_length 参数，因为它对于 ForeignKey 不适用

    def __unicode__(self):
        return self.model_name

    def __str__(self):
        return self.model_name

    class Meta:
        db_table = 'sys_model'
        verbose_name = '模块表'
        verbose_name_plural = '模块信息'


class Sys_Environment(models.Model):
    """
    环境表-马嘉璐
    """
    id = models.AutoField(primary_key=True)
    env_name = models.TextField(verbose_name='环境名称', default="")
    address = models.TextField(verbose_name='环境地址', default="")
    content = models.TextField(verbose_name='描述', default="")
    delete_flag = models.BooleanField(default=False, verbose_name='状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    def __unicode__(self):
        return self.address

    def __str__(self):
        return self.address

    class Meta:
        db_table = 'sys_environment'
        verbose_name = '环境'
        verbose_name_plural = '环境管理'

class Lyzd_Interface(models.Model):
    """
    接口信息表 - 马嘉璐
    """
    id = models.AutoField(primary_key=True)
    interface_name_en = models.TextField(verbose_name='接口api', default="")
    interface_name_zh = models.TextField(verbose_name='接口中文名称', default="")
    request_type = models.CharField(max_length=50, verbose_name='请求方式', choices=REQUEST_TYPE_CHOICE)
    authentication = models.TextField(verbose_name='认证',  choices=authentication) #'{1'：'加密解密',"0:"无认证"}
    encryption = models.TextField(verbose_name='加密的接口URL', default="")
    decrypt = models.TextField(verbose_name='解密的接口URL', default="")
    delete_flag = models.BooleanField(default=True, verbose_name='状态') # 3.0/1 哪个是删除？
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    in_param_show = models.TextField(verbose_name='入参示例', default="") #4.入参？
    out_param_show = models.TextField(verbose_name='出参示例', default="") # 5.出参？
    create_user = models.TextField(verbose_name='创建用户', default="")
    modify_user = models.TextField(verbose_name='修改用户', default="")
    content = models.TextField(verbose_name='描述', default="")
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    api_http_type = models.TextField(verbose_name='HTTP协议 http/https/webservice', default="")
    is_list = models.TextField(verbose_name='webservice时body是否为list 是Y 否N', default="")
    list_padding = models.TextField(verbose_name='webservice时body为list时', default="")
    project = models.ForeignKey('Sys_Project', on_delete=models.SET_NULL, null=True, verbose_name='项目ID')
    model = models.ForeignKey('Sys_Model', on_delete=models.SET_NULL, null=True, verbose_name='模块ID')

    def __unicode__(self):
        return self.interface_name_zh

    def __str__(self):
        return self.interface_name_zh

    class Meta:
        db_table = 'lyzd_interface'
        verbose_name = '接口信息'
        verbose_name_plural = verbose_name



class Lyzd_Interface_Param(models.Model):
    """
    接口入参表（interface_param）-马嘉璐
    """
    interface = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口外键")
    param_key = models.TextField(verbose_name='入参Key', default="")
    param_type = models.CharField(max_length=50, verbose_name="入参类型", choices=PARAM_TYPE)
    random_number = models.CharField(max_length=50, verbose_name="随机数", choices=Random_Number)
    random_number_type = models.CharField(max_length=50, verbose_name="随机数类型", choices=Random_Number_Type)
    relation = models.CharField(max_length=5, default="0", verbose_name="依赖接口", choices=relation)
    relation_interface_id = models.CharField(max_length=50, verbose_name="依赖关系接口ID")
    relation_interface_key = models.CharField(max_length=50, verbose_name="依赖关系接口Key", null=True)
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')

    #     定义表名
    class Meta:
        db_table = 'lyzd_interface_param'
        verbose_name = '接口入参数表'
        verbose_name_plural = verbose_name


# 接口header表（interface_header）：header是固定值 # 未完成
class Lyzd_Interface_Header(models.Model):

    interface = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口外键")
    header_key = models.TextField(verbose_name='Header Key', default="")
    header_key_value = models.TextField(verbose_name='Header Value', default="")
    relation = models.CharField(max_length=5, default="0", verbose_name="依赖接口", choices=relation)
    relation_interface_key = models.TextField(verbose_name='依赖关系接口Key', default="")
    relation_interface_id = models.TextField(verbose_name='依赖关系ID', default="")
    param_type = models.TextField(verbose_name='参数类型默认string', default="")
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')

    #     定义表名
    class Meta:
        db_table = 'lyzd_interface_header'
        verbose_name = '接口header表'
        verbose_name_plural = verbose_name


class Lyzd_Interface_Case(models.Model):
    """用例信息表（interface_case） -马嘉璐 """
    interface = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口外键")
    case_name = models.TextField(verbose_name='用例名称', default="")
    case_type = models.CharField(max_length=100, verbose_name="用例分类",
                                 help_text='数据校验/业务逻辑/疏通测试',
                                 choices=[('0', '数据校验'), ('1', '逻辑测试'), ('2', '疏通测试'), ('3', '全部')],
                                 default='1')
    check_type = models.CharField(max_length=100, default="0", verbose_name="校验分类", choices=Check_Type_CHOICE)
    action_condition = models.CharField(max_length=254, verbose_name="执行校验 0或 1且", default='1')
    create_user = models.TextField(verbose_name='创建用户', default="")
    modify_user = models.TextField(verbose_name='修改用户', default="")
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    delete_flag = models.BooleanField(default=False, verbose_name='状态')
    project = models.ForeignKey('Sys_Project', on_delete=models.CASCADE, verbose_name="项目外键", null=True)
    def __str__(self):
        return str(self.id)

    class Meta:
        db_table = 'lyzd_interface_case'
        verbose_name = '用例信息表'
        verbose_name_plural = verbose_name


class Lyzd_Interface_Case_Check(models.Model):
    """用例校验表 - 马嘉璐"""
    interface_cases = models.ForeignKey(Lyzd_Interface_Case, on_delete=models.CASCADE, verbose_name="测试用例外键")
    check_key = models.TextField(verbose_name='校验Key', default="")
    check_condition = models.CharField(max_length=100, verbose_name="断言分类", choices=EXAMINE_TYPE_CHOICE)
    expect_value = models.TextField(verbose_name='预期值', default="")
    check_sql = models.TextField(verbose_name='执行SQL', default="")
    create_user = models.TextField(verbose_name='创建用户', default="")
    modify_user = models.TextField(verbose_name='修改用户', default="")
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')

    #     定义表名  用例入参表
    class Meta:
        db_table = 'lyzd_interface_case_check'
        verbose_name = '用例校验表'
        verbose_name_plural = verbose_name


class Sys_Dic(models.Model):
    """字典表 （interface_case）-马嘉璐"""
    type_key = models.TextField(verbose_name='模块key', default="")
    code_id = models.TextField(verbose_name='code_id', default="")
    code_name = models.TextField(verbose_name='名称', default="")
    content = models.TextField(verbose_name='说明', default="")

    def __str__(self):
        return str(self.id)

    #     定义表名
    class Meta:
        db_table = 'sys_dic'
        verbose_name = '字典表'
        verbose_name_plural = verbose_name



class Lyzd_Interface_Case_Param(models.Model):
    """用例入参表（interface_case_param）：1个key存储1条记录 - 马嘉璐 """
    interface = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口外键")
    interface_cases = models.ForeignKey(Lyzd_Interface_Case, on_delete=models.CASCADE, verbose_name="测试用例外键")
    param_key = models.TextField(verbose_name='入参Key', default="") # 是什么形式的？{"name": "巫明瑾","age": "12"} name 和age放一起么？
    param_value = models.TextField(verbose_name='入参值Value', default="")
    content = models.TextField(verbose_name='说明', default="")
    create_user = models.TextField(verbose_name='创建用户', default="")
    modify_user = models.TextField(verbose_name='修改用户', default="")
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')

    #     定义表名  用例入参表
    class Meta:
        db_table = 'lyzd_interface_case_param'
        verbose_name = '用例入参表'
        verbose_name_plural = verbose_name

class Lyzd_Interface_Action(models.Model):
    """接口执行统计表:以接口得维度/类似于统计分析总表StatisticsData -马嘉璐"""
    project = models.ForeignKey('Sys_Project', on_delete=models.CASCADE, verbose_name="项目外键")
    environment = models.ForeignKey('Sys_Environment', on_delete=models.CASCADE, verbose_name="环境外键")
    interface_pici = models.TextField(verbose_name='批次号', default="")
    interface_action = models.TextField(verbose_name='执行的接口', default="")
    interface_action_name = models.TextField(verbose_name='执行的接口名称', default="")
    interface_action_count = models.TextField(verbose_name='执行的接口数量', default="")
    interface_action_success = models.TextField(verbose_name='成功的接口', default="")
    interface_action_success_name = models.TextField(verbose_name='成功的接口名称', default="")
    interface_action_success_count = models.TextField(verbose_name='成功的数量', default="")
    interface_action_fail = models.TextField(verbose_name='失败的接口', default="")
    interface_action_fail_name = models.TextField(verbose_name='失败的接口名称', default="")
    interface_action_fail_count = models.TextField(verbose_name='失败的接口数量', default="")
    interface_res = models.TextField(verbose_name='执行结果', default="")
    case_type = models.TextField(verbose_name='用例分类', default="")
    create_user = models.TextField(verbose_name='创建用户', default="")
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')

    #     定义表名
    class Meta:
        db_table = 'lyzd_interface_action'
        verbose_name = '接口执行统计表'
        verbose_name_plural = verbose_name



class Lyzd_InterfaceCase_Action(models.Model):
    """用例执行汇总表 -马嘉璐"""
    interface = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口外键")
    interface_case = models.ForeignKey(Lyzd_Interface_Case, on_delete=models.CASCADE, verbose_name="测试案例外键")
    interface_pici = models.TextField(verbose_name='接口执行表外键', default="")
    interface_name = models.TextField(verbose_name='接口名称', default="")
    interface_case_name = models.TextField(verbose_name='用例名称', default="")
    check_key = models.TextField(verbose_name='校验关键字', default="")
    check_condition = models.CharField(max_length=100, verbose_name="断言分类",
                                       choices=[('0', '数据校验'), ('1', '业务逻辑'), ('2', '疏通测试')], default='2')
    check_value = models.TextField(verbose_name='预期值Value', default="")
    out_value = models.TextField(verbose_name='实际返回值', default="")
    interface_res = models.TextField(verbose_name='执行结果', default="")
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')

    class Meta:
        db_table = 'lyzd_interfacecase_action'
        verbose_name = '用例执行汇总表'
        verbose_name_plural = verbose_name



class Lyzd_Interface_Action_Detail(models.Model):
    """
    # 用例执行详情表（interface_action_detail） ：以用例得维度x -马嘉璐
    """
    interfacecase_action = models.TextField(verbose_name='用例执行汇总表外键', default="")
    interface_case_name = models.TextField(verbose_name='用例名称', default="")
    check_key = models.TextField(verbose_name='校验关键字', default="")
    check_condition = models.TextField(verbose_name='断言分类', default="")
    check_value = models.TextField(verbose_name='预期值Value', default="")
    out_value = models.TextField(verbose_name='实际返回值', default="")
    action_condition = models.CharField(max_length=225, verbose_name="执行条件", choices=[('0', '或'), ('1', '与')],
                                        default='0')
    interface_res = models.TextField(verbose_name='执行结果', default="")
    param_in = models.TextField(verbose_name='入参', default="")
    param_out = models.TextField(verbose_name='出参', default="")
    create_time = models.DateTimeField(auto_now=True, verbose_name='创建时间')
    last_update_time = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')

    class Meta:
        db_table = 'lyzd_interface_action_detail'
        verbose_name = '用例执行详情表'
        verbose_name_plural = verbose_name


class Lyzd_Task(models.Model):
    """任务表 -马嘉璐"""
    interface_name = models.ForeignKey(Lyzd_Interface, on_delete=models.CASCADE, verbose_name="接口名")
    task_name = models.TextField(verbose_name='任务名', default="")
    uuid = models.TextField(verbose_name='uuid', default="")
    param_in = models.TextField(verbose_name='param_in', default="")
    carry_id = models.IntegerField(default=0)
    task_run_time_regular = models.TextField(verbose_name='定时', default="")
    ip = models.TextField(verbose_name='Environments的IP', default="")
    nosql_db = models.TextField(verbose_name='nosql_db', default="")
    db = models.TextField(verbose_name='db', default="")
    email = models.TextField(verbose_name='Email ID', default="")
    fail_count = models.TextField(verbose_name='执行失败次数', default="")
    remark = models.TextField(verbose_name='任务备注', default="")
    nosql_db_desc = models.TextField(verbose_name='nosql_db_desc', default="")
    db_remark = models.TextField(verbose_name='DB的备注', default="")
    env_name = models.TextField(verbose_name='Environments的备注', default="")
    subject = models.TextField(verbose_name='Email的标题名', default="")
    status = models.BooleanField()
    carry_status = models.IntegerField(default=2)
    update_time = models.DateTimeField(auto_now=True)
    create_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.task_name)

    class Meta:
        db_table = 'lyzd_task'
        verbose_name = '任务表'
        verbose_name_plural = verbose_name

# 第几次执行任务
class Lyzd_CarryTask(models.Model):
    """第几次执行任务记录表"""
    task_name = models.TextField(verbose_name='任务名', default="")
    html_report = models.TextField(verbose_name='html_report', default="")
    success_log_name = models.TextField(verbose_name='success_log_name', default="")
    error_log_name = models.TextField(verbose_name='error_log_name', default="")
    out_id = models.TextField(verbose_name='out_id', default="")
    step_count_all = models.IntegerField(default=0)
    step_count_now = models.IntegerField(default=0)
    update_time = models.DateTimeField(auto_now=True)
    create_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.task_name)

    class Meta:
        db_table = 'lyzd_carrytask'
        verbose_name = '执行任务记录表'
        verbose_name_plural = verbose_name





